import com from "@/utils/common.js";


function setChidData(model, data) {
  const models = model.vm.models;
  const chmodels = models.$items.filter(x => {
    return x.value.parent == model.name
  });
  chmodels.forEach(x => {
    x.value.exec("setData", data[x.value.childField]);
  });
}

export default {
  "list": function (model) {
    model.value.methods.setData = function (data) {
      let v = model.value;
      if (data == null) {
        v.data.rows = [];
      } else if (Array.isArray(data)) {
        v.data.rows = data;
      } else {
        v.data = data;
      }
      model.vlaue = v;
    }

    model.value.methods.selRows = function () {
      let gridRef = model.value.$ref;
      if (gridRef) {
        let rows = gridRef.getSelectionRows();
        return rows;
      }
      return [];
    }
  },
  "object": function (model) {
    model.value.methods.setData = function (data) {
      let v = model.value;
      v.key = data.id;
      v.data = data;
      model.value = v;
      setChidData(model.value, data);
    }
  },
  "tree": function (model) {
    model.value.methods.setData = function (data) {
      let compare = model.value.getMethod("compare") || com.utils.compare;
      let v = model.value;
      if (data == null) {
        v.data.rows = [];
      } else if (Array.isArray(data)) {
        v.data.rows = com.utils.tree(data, compare);
      } else {
        let rows = data.rows || [];
        rows = com.utils.tree(rows, compare);
        data.rows = rows;
        v.data = data;
      }
      model.vlaue = v;
    }
  }
}